<template>
	<view>
		<view class="none" v-if="isPage && list.length == 0">
			<i class="iconfont iconquesheng"></i>
			<text>暂无数据~</text>
		</view>
		<view class="addressCont">
			<view class="address" v-for="(item,index) in list" :key="index">
				<view class="address-info"><text>{{item.consignee}}</text>{{item.phone}}</view>
				<view class="address-area">
					<text v-if="item.is_default == 1">默认</text>
					{{item.province_name}}{{item.city_name}}{{item.area_name}}{{item.address}}
				</view>
				<view class="address-operate">
					<view class="address-operate-edit" @click="edit(item.unid)">编辑</view>
					<view class="address-operate-del" @click="delArea(index,item.unid)">删除</view>
				</view>
			</view>
			<uni-load-more v-if="list.length >= 8" :status="status" :content-text="contentText" />
		</view>
		<view class="bottom">
			<navigator hover-class="false" class="saveBtn" url="addArea">添加新的收货地址</navigator>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [], //地址列表
				page: 1,
				reload: true,
				isPage: false,
				status: 'more',
				contentText: {
					contentdown: '上拉加载更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多',
					contentnot: "没数据"
				}
			}
		},
		onPullDownRefresh() {
			this.publicFunc();
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		onReachBottom() {
			this.status = 'loading';
			this.page = this.page + 1;
			var _this = this;
			setTimeout(function() {
				_this.getArea();
			}, 500);
		},
		onShow() {
			this.publicFunc();
		},
		methods: {
			// 获取地址列表
			getArea() {
				this.$shopApi.apiPost('font/address/infos',{page: this.page}).then(res=>{
					if (res.data.code == 200 && res.data.data.list && res.data.data.list.length > 0) {
						var list = res.data.data.list;
						this.list = this.reload ? list : this.list.concat(list);
						this.reload = false;
						this.status = 'more';
					} else {
						if (this.reload) {
							this.list = [];
							this.isPage = true;
						} else {
							this.status = 'noMore';
						}
					}
				})
			},
			// 编辑地址
			edit(unid) {
				uni.navigateTo({
					url: '/pages/mine/addArea?unid='+unid
				})
			},
			// 删除地址
			delArea(index, unid) {
				this.$shopApi.apiPost('font/address/delete/'+unid,{unid: unid}).then(res=>{
					this.util.showDialog(res.data.message);
					if (res.data.code == 200) {
						this.list.splice(index,1);
					}
				})
			},
			// 公用方法
			publicFunc() {
				this.page = 1;
				this.reload = true;
				this.isPage = false;
				this.list = [];
				this.getArea();
			}
		}
	}
</script>

<style lang="scss">
	.addressCont {
		padding-bottom: 150upx;
	}
</style>
